package org.jradweb.core.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import org.jradweb.core.web.RequestParameterValue;



public class ErrorTag extends OutTag {
	private String className;
	
	@Override
	public int doStartTag() throws JspException {

	      this.setNeedBody(false);			// reset state related to 'default'
	      this.bodyContent = null;  // clean-up body (just in case container is pooling tag handlers)
	      
	      try {
		// print value if available; otherwise, try 'default'
		if (this.getValue() != null) {
	            out(pageContext, this.isEscapeXml(), this.getShowLength(),this.isLimitShowLength(),this.getValue());
		    return SKIP_BODY;
		}else if(this.getBean()!=null&&this.getProperty()!=null){
			RequestParameterValue rpv=this.getBean().getPropertyValues().get(this.getProperty());
			StringBuffer sb=new StringBuffer();
			if(rpv!=null && rpv.getErrorMessage()!=null){
				sb.append("<br/><span");
				if(this.getClassName()!=null){
					sb.append(" class=\"").append(className).append("\"");
				}
				else{
					sb.append(" style=\"color:red\"");
				}
				sb.append(">");
				this.pageContext.getOut().print(sb.toString());
				out(pageContext, this.isEscapeXml(), this.getShowLength(),this.isLimitShowLength(),rpv.getErrorMessage());
				this.pageContext.getOut().print("</span>");
			}
			return SKIP_BODY;
		}
		else {
		    // if we don't have a 'default' attribute, just go to the body
		    if (this.getDefault() == null) {
			this.setNeedBody( true);
			return EVAL_BODY_BUFFERED;
		    }

		    // if we do have 'default', print it
		    if (this.getDefault() != null) {
			// good 'default'
		    	out(pageContext, this.isEscapeXml(), this.getShowLength(),this.isLimitShowLength(),this.getDefault());
		    }
		    return SKIP_BODY;
		}
	      } catch (IOException ex) {
		throw new JspException(ex.toString(), ex);
	      }
	    }

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

}
